﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XingFuStudio.Pioneer.Net;
using XingFuStudio.Pioneer.Entity;
using XingFuStudio.Pioneer.Container.Interface.Event;
using System.Threading;
using System.Xml.Linq;

namespace XingFuStudio.Pioneer.Container.Interface {
    /// <summary>
    /// 各模块应用基类
    /// </summary>
    public abstract class ApplicationBase : IApplication, IPersistence{

        #region 事件  

        /// <summary>
        /// 打印日志事件
        /// </summary>
        public event EventHandler<LogEventArgs> PrintLog = null;
        /// <summary>
        /// 出错事件
        /// </summary>
        public event EventHandler<ErrorEventArgs> Error = null;
        /// <summary>
        /// 登录失败事件
        /// </summary>
        public event EventHandler<LoginFailedEventArgs> LoginFailed = null;
        /// <summary>
        /// 持久化事件
        /// </summary>
        public event EventHandler<PersistenceEventArgs> Persistence = null;

        /// <summary>
        /// 执行打印日志事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="isNewLine"></param>
        /// <param name="formate"></param>
        /// <param name="args"></param>
        protected void DoPrintLog(object sender, bool isNewLine, string format, params object[] args){
            if (this.PrintLog != null) {
                this.PrintLog(sender, new LogEventArgs() { IsNewLing = isNewLine, LogMessage = String.Format(format, args)});
            }
        }

        /// <summary>
        /// 执行出错事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="ex"></param>
        protected void DoError(object sender, Exception ex) {
            if (this.Error != null) {
                this.Error(sender, new ErrorEventArgs() { Exception = ex });
            }
        }

        /// <summary>
        /// 执行持久化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="ex"></param>
        protected void DoPersistence(object sender, XElement data) {
            if (this.Persistence != null) {
                this.Persistence(sender, new PersistenceEventArgs() {
                    PersistenceData = data
                });
            }
        }

        /// <summary>
        /// 执行登录失败事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="ex"></param>
        protected void DoLoginFailed(object sender, AccountInfo account, int errorCode, HttpManager httpManager) {
            if (this.LoginFailed != null) {
                this.LoginFailed(sender, new LoginFailedEventArgs() {
                    Account = account,
                    ErrorCode = errorCode,
                    HttpManger = httpManager
                });
            }
        }

        #endregion

        /// <summary>
        /// 应用名称
        /// </summary>
        protected string applicationName = String.Empty;
        /// <summary>
        /// 运行时间时隔（分钟为单位）
        /// </summary>
        protected int interval = 1;
        /// <summary>
        /// 执行应用的线程
        /// </summary>
        protected Thread thread = null;
        /// <summary>
        /// 是否在运行
        /// </summary>
        protected bool isRun = false;
        /// <summary>
        /// 帐户信息
        /// </summary>
        protected List<AccountInfo> accounts = null;

        #region IApplication 成员

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="appName">应用名称</param>
        public virtual void Initialize(string appName) {
            this.applicationName = appName;
        }

        /// <summary>
        /// 执行全部帐户的操作
        /// </summary>
        /// <param name="accounts">帐户集合</param>
        public virtual void Run(List<AccountInfo> accounts) {
            this.accounts = accounts;
            this.isRun = true;
        }

        /// <summary>
        /// 暂停
        /// </summary>
        public virtual void Pause() {
            if (this.thread != null) {
                try {
                    if ((this.thread.ThreadState & ThreadState.Unstarted) == 0) {
                        this.thread.Suspend();
                    }
                    if ((this.thread.ThreadState & ThreadState.Suspended) > 0) {
                        this.thread.Resume();
                    }
                } catch (Exception ex) {
                    this.DoError(this, ex);
                }
            }
        }

        /// <summary>
        /// 停止
        /// </summary>
        public virtual void Stop() {
            this.isRun = false;
            try {
                if (this.thread != null && (this.thread.ThreadState & ThreadState.Suspended) == 0) {
                    this.thread.Abort();
                }
            } catch (Exception ex) {
                this.DoError(this, ex);
            }
        }

        #endregion

        #region 属性

        /// <summary>
        /// 获取插件名称
        /// </summary>
        public string PluginName {
            get {
                return this.applicationName;
            }
        }

        #endregion

        #region IPersistence 成员

        /// <summary>
        /// 获取持久化信息的XML对象
        /// </summary>
        public virtual XElement GetPersistenceXML() {
            return null;
        }

        /// <summary>
        /// 赋值持久化信息的XML对象
        /// </summary>
        /// <param name="xml">持久化的XML</param>
        public virtual void SetPersistenceXML(XElement xml) {
            
        }

        #endregion
    }
}
